OB.Emes_ActionButton = OB.Emes_ActionButton || {};

//下载排班模板
OB.Emes_ActionButton.downloadScheduleModel = function(d,b) {
	var code= "Employee_Schedule"; 
	window.open(OB.Application.contextUrl + "downImportTemplate.html?code=" + code);
};

//排班导入
OB.Emes_ActionButton.importScheduleData = function(d,b) {
	var e = b.view.viewGrid;
	var a = e.getSelectedRecord();
	var code = "Employee_Schedule";
	var name = "排班模板导入";
	
	OB.MyActiveViewRefresh = function(){
		b.refresh();
	};
	OB.Layout.ClassicOBCompatibility.Popup.open(
		"import_Data_" + code,
		500,
		150,
		OB.Application.contextUrl + "web/com.basic/html/importData.html?code=Employee_Schedule",
		name, window, false, false, null, true, true);
//	if(a){
//	} else {
//		isc.showMessage("请选择班组！");
//	}
};

//查看员工排班情况
OB.Emes_ActionButton.groupScheduleButton = function(d, b) {
	var e = b.activeView.viewGrid;
	var a = e.getSelectedRecords();
	var c = a.getLength();
	if(c){
		var selectRecords = [];
		for (var i = 0; i < c; i++) {
			selectRecords.push(a[i].id);
		}
		OB.Emes_ActionButton.MyRefresh=function(){
			b.refresh();
		};

		OB.Emes_ActionButton.groupId = selectRecords.join("#####");
		OB.RemoteCallManager.call("com.mes.handler.GroupScheduleHandler", {
			ids : selectRecords.join("#####"),
			first: true
		}, {}, function(rpcResponse, data, rpcRequest) {
			if (!data.result) {
				alert(data.msg);
				return;
			}
			refershView = b;
			isc.External_GroupScheduleButton.create(data).show();
		});
	} else {
		isc.showMessage("请选择班组！");
	}
};

isc.defineClass('External_GroupScheduleButton',isc.OBPopup);

isc.External_GroupScheduleButton.addProperties({
	width : "70%",
	height : "70%",
	title : null,
	showMinimizeButton : false,
	showMaximizeButton : false,

	callPutData : null,
	callPutForm : null,
	okButton : null, 
	noButton : null, 
	cancelButton : true,
	queryDate : new Date(),
	week : ['日', '一', '二', '三', '四', '五', '六'],
	
	initWidget : function(data) {
		
		this.createQueryBar(data);
		this.createGrid(data);
		this.setGridValue(data.scheduleData, this.callPutForm);
		this.createUserGroupPanel();
		this.Super('initWidget',arguments);
	},
	createGrid: function(data) {
		var _self = this;

		var fields = _self.createFields(data);
	
		this.callPutForm = isc.ListGrid.create({
			title: "排班明细",
			ID: "scheduleGrid",
			width: '30%',
			height: this.height,
			fields: fields,

			selectionType: "single",
			canEdit: true,
			autoFetchData: false,
			autoSaveEdits: true,
			alternateRecordStyles: true,
			canDragRecordsOut: false,
			canAcceptDroppedRecords: false,
			canReorderRecords: true,
			canReorderFields: false,
			canResizeFields: true,
//			canFreezeFields: true,
			recordClick: function(grid, data, index) {
			}
		});
	},
	createQueryBar: function(data) {
		var _self = this;
		this.queryMonth = isc.DynamicForm.create({
			numCols : 2,
			fields : [ {
				name : 'queryMonth',
				title : '月份',
				height : 20,
				width : 150,
				wrapTitle: false,
				value : _self.queryDate,
				type : '_id_15' //Date reference
			} ]
		});
		
		var empAll = data.groupAllEmployee || [];
		var selMap = {
				_null : ""
		};
		for(var i = 0; i<empAll.length; i++){
			selMap[empAll[i].EMES_EMPLOYEE_ID] = empAll[i].NAMES;
		}
		
		var orgALL = data.getGroupOrgs || [];
		var orgMap = {
				_null : ""
		};
		for(var i = 0; i<orgALL.length; i++){
			orgMap[orgALL[i].AD_ORG_ID] = orgALL[i].NAME;
		}
		
		var groupALL = data.getGroupNames || [];
		var groupMap = {
				_null : ""
		};
		for(var i = 0; i<groupALL.length; i++){
			groupMap[groupALL[i].NAMEID] = groupALL[i].NAMEID;
		}
		
		this.orgName = isc.DynamicForm.create({
			numCols : 2,
			fields : [ {
				name : 'orgName',
				title : '部门',
				height : 20,
				width : 150,
				wrapTitle: false,
				canEdit: true,
				canEditCriterion: true,
				canEditOpaqueValues: true,
				valueMap: orgMap,
				type : 'select' //Date reference
			} ]
		});
		
		this.groupName = isc.DynamicForm.create({
			numCols : 2,
			fields : [ {
				name : 'groupName',
				title : '班组',
				height : 20,
				width : 150,
				wrapTitle: false,
				canEdit: true,
				canEditCriterion: true,
				canEditOpaqueValues: true,
				valueMap: groupMap,
				type : 'select' //Date reference
			} ]
		});
		
		
		this.queryName = isc.DynamicForm.create({
			numCols : 2,
			fields : [ {
				name : 'queryName',
				title : '员工',
				height : 20,
				width : 150,
				wrapTitle: false,
				canEdit: true,
				canEditCriterion: true,
				canEditOpaqueValues: true,
				valueMap: selMap,
				type : 'select' //Date reference
			} ]
		});
		this.queryButton = isc.OBFormButton.create({
			title : '查询',
			popup : this,
			action : function() {
				var month = this.popup.queryMonth.getField('queryMonth').getValue();
				var name = this.popup.queryName.getField('queryName').getValue();
				var ORGnames = this.popup.orgName.getField('orgName').getValue();
				var groupnames = this.popup.groupName.getField('groupName').getValue();
				
				_self.queryDate = month;
				OB.RemoteCallManager.call("com.mes.handler.GroupScheduleHandler", {
					ids : OB.Emes_ActionButton.groupId,
					month : month,
					employeeId : name || "",
					orgId : ORGnames || "",
					groupName : groupnames || "",
				}, {}, function(rpcResponse, data, rpcRequest) {
					if (!data.result) {
						alert(data.msg);
						return;
					} else {
						_self.createFields(data, _self.callPutForm);
						_self.setGridValue(data.scheduleData, _self.callPutForm);
					}
				});
			}
		});
		
		/*this.queryButtons = isc.OBFormButton.create({
			title : '审核',
			popup : this,
			action : function() {
				var month = this.popup.queryMonth.getField('queryMonth').getValue();
				var name = this.popup.queryName.getField('queryName').getValue();
				_self.queryDate = month;
				OB.RemoteCallManager.call("com.mes.handler.GroupScheduleHandler", {
					ids : OB.Emes_ActionButton.groupId,
					month : month,
					employeeId : name || ""
				}, {}, function(rpcResponse, data, rpcRequest) {
					if (!data.result) {
						alert(data.msg);
						return;
					} else {
						_self.createFields(data, _self.callPutForm);
						_self.setGridValue(data.scheduleData, _self.callPutForm);
					}
				});
			}
		});*/
	},
	createUserGroupPanel: function() {
		var _self = this;
		
		// OK Button
		this.okButton = isc.OBFormButton.create({
			title : '审核通过',
			popup : this,
			action : function() {
				scheduleGrid.saveEdits();
				var upResult = scheduleGrid.data;

				var callback = function(rpcResponse, data, rpcRequest) {
					if (data.flag) {
//						refershView.refresh();
						isc.showMessage(data.message,
								isc.OBMessageBar.TYPE_INFO);
						_self.queryButton.click();
//						rpcRequest.clientContext.popup.closeClick();
					} else {
						isc.showMessage(data.message,
								isc.OBMessageBar.TYPE_INFO);
					}
//					OB.Emes_ActionButton.MyRefresh();

				};
				OB.RemoteCallManager.call(
						"com.mes.handler.GroupScheduleSaveButtonHandler", {
							dateParam : upResult, resultStatus : 'OK'
						}, {}, callback);
//				this.popup.closeClick();
			}
		});
		
		// OK Button
		this.noButton = isc.OBFormButton.create({
			title : '审核不通过',
			popup : this,
			action : function() {
				scheduleGrid.saveEdits();
				var upResult = scheduleGrid.data;

				var callback = function(rpcResponse, data, rpcRequest) {
					if (data.flag) {
						isc.showMessage(data.message,
								isc.OBMessageBar.TYPE_INFO);
						_self.queryButton.click();
					} else {
						isc.showMessage(data.message,
								isc.OBMessageBar.TYPE_INFO);
					}
				};
				OB.RemoteCallManager.call(
						"com.mes.handler.GroupScheduleSaveButtonHandler", {
							dateParam : upResult, resultStatus : 'NO'
						}, {}, callback);
			}
		});

		// Cancel Button
		this.cancelButton = isc.OBFormButton.create({
			title : '取消',
			popup : this,
			action : function() {
				this.popup.closeClick();
			}
		});

		// Add the elements into a layout
		this.items = [ isc.VLayout.create({
			defaultLayoutAlign : "center",
			align : "center",
			width : "100%",
			layoutMargin : 15,
			membersMargin : 15,
			members : [ isc.HLayout.create({
				defaultLayoutAlign : "center",
				align : "left",
				membersMargin : 30,
				marginBottom : 10,
			//	members : [ this.queryMonth,this.orgName ,this.groupName ,this.queryName, this.queryButton ]
				members : [ this.queryMonth, this.queryButton ]
			}), isc.HLayout.create({
				defaultLayoutAlign : "center",
				align : "center",
				width : "100%",
				height : "80%",
				members : [ this.callPutForm ]
			}), isc.HLayout.create({
				defaultLayoutAlign : "center",
				align : "center",
				membersMargin : 10,
				marginBottom : 10,
				members : [ this.okButton,this.noButton, this.cancelButton ]
			}) ]
		}) ];
		
	},
	setGridValue: function(data, grid){
		var rows = grid.data;
		for (var i = 0;i < rows.length;i ++) {
			grid.removeData(rows[i]);
		}
		grid.setData(data);
	},
	createFields: function(data, grid) {
		var _self = this;
//		day_shift: '白',
//		night_shift: '晚',
//		rest: '休',
//		leave: '假'
		var selData = {
			_null: ''
			
		};
		var selDatas = {
				_null: ''
				
			};
		var st = data.scheduleType || [];
		for(var i = 0; i < st.length; i++){
				selData[st[i].VALUE] = st[i].NAME;
		}
		selDatas['OK'] = "<font color='red'>"+'已审核'+"</font>";
		selDatas['CREATE'] = "<font color='blue'>"+'未审核'+"</font>";
		var fields = [
			{width: '70', title: '月份', name: 'MON',canEdit: false, autoFreeze: true},
			{width: '120', title: '部门', name: 'ORGNAMES',canEdit: false, autoFreeze: true},
			{width: '120', title: '班组', name: 'GROUPNAME',canEdit: false, autoFreeze: true},
			{width: '100', title: '员工姓名', name: 'NAMES',canEdit: false, autoFreeze: true}
		];
		var d = new Date(_self.queryDate);
		for(var i = 1; i < 32; i++){
			d.setDate(i);	//设置日期
			fields.push({
				width: '70',
				type: 'select',
				title: i + ' (周' + _self.week[d.getDay()] + ')', 
				name: 'SCHEDULETYPE_' + (i < 10 ? '0' + i : i), 
				valueMap: selData , 
				readOnlyDisplay:"static",
				startRow:true
			});
			fields.push({
				width: '70',
				type: 'select',
				//title: i + ' (周' + _self.week[d.getDay()] + ')', 
				title:' ',
				name: 'STATUS_' + (i < 10 ? '0' + i : i), 
				readOnlyDisplay:"static",
				valueMap: selDatas , 
				startRow:true,
				canEdit: false
			});
		}
		if(grid){
			grid.setFields(fields);
		}
		return fields;
	}
});